# BUILD FILE SYNTAX: SKYLARK

import json

mbgl_base_path = get_base_path() or '.'

def mbgl_load_file_lists(*argv):
    merged = dict(sources = [], public_headers = {}, private_headers = {})
    for arg in argv:
        filename = mbgl_base_path + '/' + arg + '-files.json'
        add_build_file_dep('//' + filename)
        files = json.load(open(filename))
        merged['public_headers'].update(files['public_headers'])
        merged['private_headers'].update(files['private_headers'])
        merged['sources'] += files['sources']
    return merged

mbgl = dict(
    core = mbgl_load_file_lists('src/core', 'platform/default/filesource'),
    android = mbgl_load_file_lists('platform/android/core', 'platform/android/filesource'),
    ios = mbgl_load_file_lists('platform/ios/core', 'platform/ios/sdk'),
    deps = [],
)

def mbgl_vendor_library(name):
    files = mbgl_load_file_lists('vendor/' + name)
    cxx_library(
        name = "vendor-" + name,
        header_namespace = "",
        srcs = files['sources'],
        headers = files['private_headers'],
        exported_headers = files['public_headers'],
        link_style = 'static',
        compiler_flags = [
            "-Wno-tautological-unsigned-enum-zero-compare",
        ],
    )
    mbgl['deps'].append(':vendor-' + name)

mbgl_vendor_library("icu")
mbgl_vendor_library("boost")
mbgl_vendor_library("cheap-ruler-cpp")
mbgl_vendor_library("earcut.hpp")
mbgl_vendor_library("eternal")
mbgl_vendor_library("geojson-vt-cpp")
mbgl_vendor_library("mapbox-base")
mbgl_vendor_library("sqlite")
mbgl_vendor_library("polylabel")
mbgl_vendor_library("protozero")
mbgl_vendor_library("shelf-pack-cpp")
mbgl_vendor_library("unique_resource")
mbgl_vendor_library("vector-tile")
mbgl_vendor_library("wagyu")

cxx_library(
    name = "core",

    srcs = mbgl['core']['sources'],
    platform_srcs = [
        ("android", mbgl['android']['sources']),
    ],

    header_namespace = "",
    headers = mbgl['core']['private_headers'],
    exported_headers = mbgl['core']['public_headers'],
    platform_headers = [
        ("android", mbgl['android']['private_headers'])
    ],
    exported_platform_headers = [
        ("android", mbgl['android']['public_headers']),
    ],

    compiler_flags = [
        "-std=c++14",
        "-g",
        "-Wall",
        "-Wextra",
        "-Werror",
        "-Wno-unused-variable",
        "-Wno-unused-parameter",
        "-Wno-c++11-narrowing",
        "-Wno-tautological-constant-compare",
        "-fexceptions",
        "-ftemplate-depth=1024",
        "-frtti",
    ],

    preprocessor_flags = [
        "-DMBGL_USE_GLES2=1",
        "-DMBGL_VERSION_REV=\"standalone\"",
        "-DRAPIDJSON_HAS_STDSTRING=1",
    ],
    exported_platform_linker_flags = [
        ("android", [
            "-lz",
            "-lEGL",
            "-lGLESv2",
            "-landroid",
            "-ljnigraphics",
            "-llog",
            "-fvisibility=hidden",
            "-Wl,--icf=safe",
            "-Wl,--version-script=" + mbgl_base_path + "/platform/android/version-script"
        ]),
    ],

    visibility = ["PUBLIC"],
    deps = mbgl['deps'],
    soname = "libmapbox-gl.$(ext)",
)

ndk_library(
    name = "android-core",
    deps = [ ":core" ],
    flags = [
        "NDK_APPLICATION_MK=$(env PWD)/" + mbgl_base_path + "/Application.mk",
    ],
)

android_manifest(
  name = 'android-manifest',
  skeleton = 'platform/android/MapboxGLAndroidSDK/src/main/AndroidManifest.xml',
)

android_library(
    name = "android",
    deps = [ ":android-core" ],
    manifest = ":android-manifest",
)

apple_library(
    name = "ios-core",
    deps = [ ":core" ],
    headers = mbgl['ios']['private_headers'],
    exported_headers = mbgl['ios']['public_headers'],
    srcs = mbgl['ios']['sources'],
    lang_compiler_flags = {
        "OBJCXX_CPP_OUTPUT": [
            "-std=c++14",
            "-fmodules",
            "-fobjc-arc",
        ],
        "OBJC_CPP_OUTPUT": [
            "-fmodules",
            "-fobjc-arc",
        ],
        "CXX_CPP_OUTPUT": [
            "-std=c++14",
        ]
    },
)
